<template>
  <div class="login">
    <h3>登录</h3>
    <el-form ref="form" :model="loginData" :rules="rules">
      <el-form-item prop="username"
        ><el-input
          v-model="loginData.username"
          placeholder="请输入账号"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password"
        ><el-input
          v-model="loginData.password"
          type="password"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-button type="primary" @click="checkForm('form')">登录</el-button>
    </el-form>
  </div>
</template>

<script>
import { adminLogin } from "../../api/index";
import { StorageName } from "../../config/localStorage";
export default {
  name: "login",
  data() {
    return {
      loginData: {
        username: "",
        password: "",
      },
      rules: {
        username: {
          required: true,
          message: "请输入账号",
          trigger: "blur",
        },
        password: {
          required: true,
          message: "请输入密码",
          trigger: "blur",
        },
      },
    };
  },
  methods: {
    checkForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (!valid) return;
        this.submit();
      });
    },
    submit() {
      adminLogin(this.loginData).then((res) => {
        if (res) {
          this.$storage.saveStorage(StorageName.Token, res.data.token);
          this.$router.replace("/menu");
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  .w(400);
  margin: 0 auto;
  button {
    width: 100%;
  }
  h3 {
    text-align: center;
    .mg(50px auto 50px);
  }
}
</style>